package com.yuqiao.read.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

public class Lambda2 {

    public static <T> List<T> fiterList(List<T> list, Predicate<T> p) {
        List<T> result = new ArrayList<T>();
        for (T e : list) {
            if (p.test(e)) {
                result.add(e);
            }
        }
        return result;
    }


    public static void main(String[] args) {
//        List<String> nums = new ArrayList<String>();
//        nums.add("1");
//        nums.add("2");
//        nums.add("3");
//        nums.add("4");
//        List<String> strings = fiterList(nums, (String i) -> "2".equals(i));
//        System.out.println("args = [" + strings + "]");
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));


        List<String> dishNames = menu.stream()
                .filter((Dish d) -> d.getCalories() > 300)
                .map(Dish::getName)
                .limit(3)
                .collect(toList());


        System.out.println(menu.stream().filter(d-> d.getCalories()>300)
                .map(Dish::getName)
                .skip(3)
                .collect(toList()));




        List<String> dnames = menu.stream()
                .filter((Dish d) -> {
                    System.out.println("filtering = [" + d.getName() + "]");
                    return d.getCalories() > 300;})
                .map(dish ->{
                    System.out.println("maping = [" + dish.getName() + "]");
                    return dish.getName();
                })
                .limit(3)
                .collect(toList());
//        System.out.println("args = [" + dishNames + "]");
//        menu.stream().forEach(System.out::println);
        menu.stream().filter(Dish::isVegetarian).collect(toList());
        double price = 232.5;
        List<Integer> numbers = Arrays.asList(1,2,3,4,4,6,7,8,8,10);
        numbers.stream().reduce(0,(a,b)->a*b);
        numbers.stream().reduce(0,Integer::sum);
        List<Integer> oushu = numbers.stream().filter(i -> i % 2 == 0).distinct().collect(toList());
//        System.out.println("args = [" + oushu + "]");
        List<String> strings = Arrays.asList("hello","java","haha");
        System.out.println(strings.stream().map(String::length).collect(toList()));

        Optional<Dish> any = menu.stream().filter(Dish::isVegetarian).findAny();
        System.out.println("args = [" + any + "]");
    }

}
